package com.gfwy.servlet.admin;

import java.io.IOException;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.gfwy.factory.DaoFactory;
import com.gfwy.vo.Period;


public class AddPeriod extends HttpServlet {

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String pid = request.getParameter("pid");
		String pname = request.getParameter("pname");
		String prelude = request.getParameter("prelude");
		String title = request.getParameter("title");
		String author = request.getParameter("author");
		boolean flag = false;
		if(!(pid == null || "".equals(pid))) {
			try {
				Period p = new Period();
				p.setPid(Integer.parseInt(pid));
				p.setPname(pname);
				p.setPrelude(prelude);
				p.setTitle(title);
				p.setAuthor(author);
				flag = DaoFactory.getIPeriodDaoInstance().doUpdate(p);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			try {
				Period p = new Period();
				p.setPname(pname);
				p.setPrelude(prelude);
				p.setTitle(title);
				p.setAuthor(author);
				flag = DaoFactory.getIPeriodDaoInstance().doCreate(p);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		if(flag == true) {
			response.sendRedirect(request.getContextPath() + "/admin/plist");
		}
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doGet(request, response);
	}

}
